#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void OnConnection(const PtrConnect &conn)
    {
        DLOG("OnConnection: %p", conn.get());
    }

    void CloseConnection(const PtrConnect &conn)
    {
        DLOG("CloseConnection: %p", conn.get());
    }

    void OnMessage(const PtrConnect &con, Buffer *buf)
    {
        con->SendCon(buf->GetReaderPos(),buf->ReadAbleSize());//从哪里开始发，和发送多少数据
        buf->ReadMoveOffset(buf->ReadAbleSize());
        con->Shutdown(); // 运行一次就关闭连接
    }

public:
    EchoServer(int port)
        : _server(port)
    {
        _server.SetConnectCallBack(std::bind(&EchoServer::OnConnection, this, std::placeholders::_1));
        _server.SetCloseCallBack(std::bind(&EchoServer::CloseConnection, this, std::placeholders::_1));
        _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }

    void Start()
    {
        _server.Start();
    }
};